package com.culturalCenter.dataCenter.service.impl.wifilocation;

import com.culturalCenter.dataCenter.Utils.RestClientUtil;
import com.culturalCenter.dataCenter.customEntity.responseParam.wifilocation.WifiLocationResponse;
import com.culturalCenter.dataCenter.entity.user.User;
import com.culturalCenter.dataCenter.mapper.user.UserMapper;
import com.culturalCenter.dataCenter.service.wifilocation.WifiLocationService;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * @Author zhao
 * @Date 2020/12/21 11:36
 */
@Service
@Slf4j
public class WifiLocationServiceImpl implements WifiLocationService {

    @Resource
    UserMapper userMapper;

    @Override
    public WifiLocationResponse getLocationInfo(String id) {
        WifiLocationResponse wifiLocationResponse = new WifiLocationResponse();
        // 根据用户id查询该用户的手机号
        User user = userMapper.queryById(id);
        String userPhone = user.getPhone();

        // 定义主机名和端口名
        String host = "192.168.0.197";
        int port = 8080;

        // 手机号作为账号名，调用rest中根据账号名查询接入明细接口获取用户macAddress
        String uri1 = "/imcrs/uam/acmUser/acmDetailList/" + userPhone;
        String result1 = RestClientUtil.callInterface(host, port, uri1);
        Document doc1 = null;
        try {
            doc1 = DocumentHelper.parseText(result1);
        } catch (DocumentException e) {
            log.error(e.getMessage(), e);
        }
        Element list = doc1.getRootElement();// 获取跟节点
        String macAddress = list.element("acmDetail").element("macAddress").getTextTrim();

        // 根据查出来的macAddress，调用rest中根据MAC地址查询位置信息接口获取用户的locName、posX、posY
        String uri2 = "/imcrs/wlan/loc/getLocationByMAC?mac=" + macAddress;
        String result2 = RestClientUtil.callInterface(host, port, uri2);
        Document doc2 = null;
        try {
            doc2 = DocumentHelper.parseText(result2);
        } catch (DocumentException e) {
            log.error(e.getMessage(), e);
        }
        // 获取跟节点
        Element location = doc2.getRootElement();
        String locName = location.element("locName").getTextTrim();
        String posXReturn = location.element("posX").getTextTrim();
        int posX = Integer.parseInt(posXReturn);
        String posYReturn = location.element("posY").getTextTrim();
        int posY = Integer.parseInt(posYReturn);

        // 根据查出来的locName，调用rest中根据位置视图名称获取比例尺信息接口获取scale(比例尺)、scaleType（比例尺类型）
        String uri3 = "/imcrs/wlan/loc/getScaleByLocName?locName=" + locName;
        String result3 = RestClientUtil.callInterface(host, port, uri3);
        Document doc3 = null;
        try {
            doc3 = DocumentHelper.parseText(result3);
        } catch (DocumentException e) {
            log.error(e.getMessage(), e);
        }
        Element list3 = doc3.getRootElement();// 获取跟节点
        String scaleReturn = list3.element("scale").getTextTrim();
        double scale = Integer.parseInt(scaleReturn);
        String scaleTypeReturn = list3.element("scaleType").getTextTrim();
        int scaleType = Integer.parseInt(scaleTypeReturn);

        // 根据posX、posY、scale、scaleType计算横向和纵向距离(距离的最后结果单位统一为米)
        if (scaleType == 0) {
            double dx = (posX * scale) / 100;
            double dy = (posY * scale) / 100;
            wifiLocationResponse.setDx(dx);
            wifiLocationResponse.setDy(dy);
        }
        if (scaleType == 1) {
            double dx = (posX * scale) / 100 * 0.3048; // 把英尺转化为米，下同
            double dy = (posY * scale) / 100 * 0.3048;
            wifiLocationResponse.setDx(dx);
            wifiLocationResponse.setDy(dy);
        }

        return wifiLocationResponse;
    }

    @Override
    public List<WifiLocationResponse> getOnlineLocationInfo() {
        List<WifiLocationResponse> locationResponseList = new ArrayList<>();

        // 定义主机名和端口名
        String host = "192.168.0.197";
        int port = 8080;

        // 调用rest中查询在线用户列表接口获取各个用户macAddress
        String uri = "/uam/online";
        String result1 = RestClientUtil.callInterface(host, port, uri);
        Document doc1 = null;
        try {
            doc1 = DocumentHelper.parseText(result1);
        } catch (DocumentException e) {
            log.error(e.getMessage(), e);
        }
        Element list = doc1.getRootElement();// 获取跟节点
        // 处理xml循环节点
        List<Element> onlineList = list.selectNodes("online");
        for (Element single : onlineList) {
            String macAddress = single.element("macAddress").getTextTrim();

            // 根据查出来的macAddress，调用rest中根据MAC地址查询位置信息接口获取用户的locName、posX、posY
            String uri2 = "/imcrs/wlan/loc/getLocationByMAC?mac=" + macAddress;
            String result2 = RestClientUtil.callInterface(host, port, uri2);
            Document doc2 = null;
            try {
                doc2 = DocumentHelper.parseText(result2);
            } catch (DocumentException e) {
                log.error(e.getMessage(), e);
            }
            Element location = doc2.getRootElement();// 获取跟节点
            String locName = location.element("locName").getTextTrim();
            String posXReturn = location.element("posX").getTextTrim();
            int posX = Integer.parseInt(posXReturn);
            String posYReturn = location.element("posY").getTextTrim();
            int posY = Integer.parseInt(posYReturn);

            // 根据查出来的locName，调用rest中根据位置视图名称获取比例尺信息接口获取scale(比例尺)、scaleType（比例尺类型）
            String uri3 = "/imcrs/wlan/loc/getScaleByLocName?locName=" + locName;
            String result3 = RestClientUtil.callInterface(host, port, uri3);
            Document doc3 = null;
            try {
                doc3 = DocumentHelper.parseText(result3);
            } catch (DocumentException e) {
                log.error(e.getMessage(), e);
            }
            Element list3 = doc3.getRootElement();// 获取跟节点
            String scaleReturn = list3.element("scale").getTextTrim();
            double scale = Integer.parseInt(scaleReturn);
            String scaleTypeReturn = list3.element("scaleType").getTextTrim();
            int scaleType = Integer.parseInt(scaleTypeReturn);

            // 根据posX、posY、scale、scaleType计算横向和纵向距离(距离的最后结果单位统一为米)
            if (scaleType == 0) {
                double dx = (posX * scale) / 100;
                double dy = (posY * scale) / 100;
                locationResponseList.add(new WifiLocationResponse(dx, dy));
            }
            if (scaleType == 1) {
                double dx = (posX * scale) / 100 * 0.3048; // 把英尺转化为米，下同
                double dy = (posY * scale) / 100 * 0.3048;
                locationResponseList.add(new WifiLocationResponse(dx, dy));
            }
        }

        return locationResponseList;
    }

}
